package org.edu.modules.act.service.impl;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.lang3.StringUtils;
import org.edu.modules.act.common.result.PaginationBuilder;
import org.edu.modules.act.service.VeActiPublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务信息的业务逻辑实现层
 *
 * @版权所有 福州土著人信息科技有限公司
 */
@Service
@Transactional
public class VeActiPublicServiceImpl implements VeActiPublicService {

    private static final String FINISHED = "finished";
    private static final String ASSIGNEE = "assignee";
    private static final String CANDIDATE = "candidate";

    @Autowired
    private VeActiPublicService veActiPublicService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private RuntimeService runtimeService;

    @Override
    public Map<String, Object> queryTask(String taskName) {
        TaskQuery taskQuery = taskService.createTaskQuery();
        if (StringUtils.isNotBlank(taskName)) {
            taskQuery.taskNameLike("%" + taskName + "%");
        }
        List<Task> taskList = taskQuery.includeProcessVariables().includeTaskLocalVariables().orderByTaskPriority().desc()
                .listPage(0, 1000000);

        List<LinkedHashMap<String, Object>> resultList = new ArrayList<>();
        for (int i = 0; i < taskList.size(); i++) {
            LinkedHashMap<String, Object> taskMap = new LinkedHashMap<>();
            taskMap.put("id", taskList.get(i).getId());
            taskMap.put(ASSIGNEE, taskList.get(i).getAssignee());
            taskMap.put("category", taskList.get(i).getCategory());
            taskMap.put("claimTime", taskList.get(i).getClaimTime());
            taskMap.put("createTime", taskList.get(i).getCreateTime());
            taskMap.put("delegationState", taskList.get(i).getDelegationState());
            taskMap.put("description", taskList.get(i).getDescription());
            taskMap.put("dueDate", taskList.get(i).getDueDate());
            taskMap.put("executionId", taskList.get(i).getExecutionId());
            taskMap.put("formKey", taskList.get(i).getFormKey());
            taskMap.put("suspended", taskList.get(i).isSuspended());
            taskMap.put("name", taskList.get(i).getName());
            taskMap.put("owner", taskList.get(i).getOwner());
            taskMap.put("parentTaskId", taskList.get(i).getParentTaskId());
            taskMap.put("priority", taskList.get(i).getPriority());
            taskMap.put("processDefinitionId", taskList.get(i).getProcessDefinitionId());
            taskMap.put("processInstanceId", taskList.get(i).getProcessInstanceId());
            taskMap.put("processVariables", taskList.get(i).getProcessVariables());
            taskMap.put("taskDefinitionKey", taskList.get(i).getTaskDefinitionKey());
            taskMap.put("taskLocalVariables", taskList.get(i).getTaskLocalVariables());
            taskMap.put("tenantId", taskList.get(i).getTenantId());
            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(taskList.get(i).getProcessInstanceId())
                    .singleResult();
            taskMap.put("processDefinitionName", processInstance.getProcessDefinitionName());
            taskMap.put("startUserId", processInstance.getStartUserId());
            List<Comment> commentList = taskService.getProcessInstanceComments(taskList.get(i).getProcessInstanceId());// 查询该流程实例ID的所有批注 NOSONAR
            List<String> comments = new ArrayList<>();
            for (int j = 0; j < commentList.size(); j++) {
                comments.add(commentList.get(j).getUserId() + "：" + commentList.get(j).getFullMessage() + "\n");
            }

            taskMap.put("comments", comments);
            resultList.add(taskMap);
        }


        return PaginationBuilder.buildResult(resultList, taskQuery.count(), 0, 10000);
    }



}
